package com.pineapple.module.core.hithormone.service;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import com.pineapple.module.core.hithormone.domain.Hithormone;
import com.pineapple.module.core.hithormone.domain.HithormoneCriteria;

public class HithormoneDAO {

	protected int count(HithormoneCriteria criteria,Connection conn) throws Exception{
 		int totRecord =0;

		StringBuilder sql = new StringBuilder();
		sql.append("SELECT COUNT(*) AS TOT  ");
		sql.append("FROM ");
		sql.append("hithormone as h ");
		sql.append("Inner Join hormone as f ON h.hithormone_hormone = f.hormone_id ");
		sql.append("Inner Join planting ON h.hithormone_area = planting.planting_id ");
		sql.append("Inner Join area ON planting.area_id = area.area_id ");
		sql.append("WHERE 1=1 ");

		if(criteria.getHithormoneId()!=null && !criteria.getHithormoneId().equals("")){
			sql.append("AND h.hithormone_Id='"+criteria.getHithormoneId()+"' ");
		}
		if(criteria.getHithormoneArea()!=null && !criteria.getHithormoneArea().equals("")){
			sql.append("AND h.hithormone_area='"+criteria.getHithormoneArea()+"' ");
		}
		if(criteria.getHithormoneDateFrom()!=null && !criteria.getHithormoneDateFrom().equals("")){
			sql.append("AND h.hithormone_date  >='"+dateForInsert(criteria.getHithormoneDateFrom())+"' ");
		}
		if(criteria.getHithormoneDateTo()!=null && !criteria.getHithormoneDateTo().equals("")){
			sql.append("AND h.hithormone_date <='"+dateForInsert(criteria.getHithormoneDateTo())+"' ");
		}
		if(criteria.getHithormoneHormone()!=null && !criteria.getHithormoneHormone().equals("")){
			sql.append("AND h.hithormone_hormone ='"+criteria.getHithormoneHormone()+"' ");
		}

		try(Statement stmt = conn.createStatement()){
			ResultSet rst = stmt.executeQuery(sql.toString());
			if (rst.next()) {
				totRecord = rst.getInt("TOT");
			}
		}catch(Exception ex){
			ex.printStackTrace();
			throw ex;
		}
		return totRecord;
	}



	protected List<Hithormone> search(HithormoneCriteria criteria,Connection conn) throws Exception{
		List<Hithormone> lstHithormone = new ArrayList<Hithormone>();
		Hithormone hithormone = null;
		StringBuilder sql = new StringBuilder();
		sql.append("SELECT ");
		sql.append("f.hormone_name,");
		sql.append("h.hithormone_Id,");
		sql.append("h.hithormone_date,");
		sql.append("h.hithormone_hormone,");
		//sql.append("h.hithormone_area,");
		sql.append("h.hithormone_value,");
		sql.append("planting.planting_status, ");
		sql.append("planting.area_id, ");
		sql.append("planting.planting_id, ");
		sql.append("area.area_name ");
		sql.append("FROM ");
		sql.append("hithormone as h ");
		sql.append("Inner Join hormone as f ON h.hithormone_hormone = f.hormone_id ");
		sql.append("Inner Join planting ON h.hithormone_area = planting.planting_id ");
		sql.append("Inner Join area ON planting.area_id = area.area_id ");
		sql.append("WHERE 1=1 ");

		if(criteria.getHithormoneId()!=null && !criteria.getHithormoneId().equals("")){
			sql.append("AND h.hithormone_Id='"+criteria.getHithormoneId()+"' ");
		}
		if(criteria.getHithormoneArea()!=null && !criteria.getHithormoneArea().equals("")){
			sql.append("AND h.hithormone_area='"+criteria.getHithormoneArea()+"' ");
		}
		if(criteria.getHithormoneDateFrom()!=null && !criteria.getHithormoneDateFrom().equals("")){
			sql.append("AND h.hithormone_date  >='"+dateForInsert(criteria.getHithormoneDateFrom())+"' ");
		}
		if(criteria.getHithormoneDateTo()!=null && !criteria.getHithormoneDateTo().equals("")){
			sql.append("AND h.hithormone_date <='"+dateForInsert(criteria.getHithormoneDateTo())+"' ");
		}
		if(criteria.getHithormoneHormone()!=null && !criteria.getHithormoneHormone().equals("")){
			sql.append("AND h.hithormone_hormone ='"+criteria.getHithormoneHormone()+"' ");
		}

		System.out.println(sql.toString());

		try(Statement stmt = conn.createStatement()){
			  ResultSet rs = stmt.executeQuery(sql.toString());
		        while(rs.next()) {
		        	hithormone= new Hithormone();
		        	hithormone.setHithormoneHormoneName(rs.getString("hormone_name"));
		        	hithormone.setHithormoneArea(rs.getString("area_name"));
		        	hithormone.setHithormoneId(rs.getString("hithormone_Id"));
		        	hithormone.setHithormoneValue(rs.getString("hithormone_value"));
		        	hithormone.setPlantingStatus(rs.getString("planting_status"));
		        	hithormone.setHithormoneDate(dateForShow(rs.getDate("hithormone_date",Calendar.getInstance(Locale.ENGLISH))));
		        	lstHithormone.add(hithormone);
		        }
		}catch(Exception ex){
			ex.printStackTrace();
			throw ex;
		}
		return lstHithormone;
	}

	protected Map<String,String> hormoneCombo(Connection conn)throws Exception{
		Map<String,String> lstPineapple = new HashMap<String, String>();
		StringBuilder sql = new StringBuilder();
		sql.append("SELECT * FROM hormone ");
		try(Statement stmt = conn.createStatement()){
			  ResultSet rs = stmt.executeQuery(sql.toString());
		        while(rs.next()) {
		        	lstPineapple.put(rs.getString("hormone_id"),rs.getString("hormone_name"));
		        }
		}catch(Exception ex){
			ex.printStackTrace();
			throw ex;
		}
		return lstPineapple;
	}

	protected Map<String,String> areaCombo(Connection conn)throws Exception{
		Map<String,String> lstArea = new HashMap<String, String>();
		StringBuilder sql = new StringBuilder();
		sql.append("SELECT * FROM area	 ");
		try(Statement stmt = conn.createStatement()){
			ResultSet rs = stmt.executeQuery(sql.toString());
			while(rs.next()) {
				lstArea.put(rs.getString("Area_id"),rs.getString("Area_name"));
			}
		}catch(Exception ex){
			ex.printStackTrace();
			throw ex;
		}
		return lstArea;
	}



	protected void add(Connection conn, Hithormone hithormone)throws Exception{
		StringBuilder sql = new StringBuilder();
		sql.append("INSERT INTO hithormone (hithormone_date,hithormone_hormone" +
				",hithormone_area,hithormone_value ) ");
		sql.append("VALUES ( " );
		sql.append("'"+dateForInsert(hithormone.getHithormoneDate())+"' ");
		sql.append(",'"+hithormone.getHithormoneHormone()+"' ");
		sql.append(",'"+hithormone.getHithormoneAreaId()+"' ");
		sql.append(",'"+hithormone.getHithormoneValue()+"' ");
		sql.append(" )");
		System.out.println(sql.toString());
		try(Statement stmt = conn.createStatement()){
			 stmt.executeUpdate(sql.toString());
		}catch(Exception ex){
			ex.printStackTrace();
			throw ex;
		}
	}


	protected void edit(Connection conn, Hithormone hithormone)throws Exception{

		StringBuilder sql = new StringBuilder();
		sql.append("UPDATE hithormone SET ");
		sql.append(" hithormone_date='"+dateForInsert(hithormone.getHithormoneDate())+"' ");
		sql.append(",hithormone_hormone='"+hithormone.getHithormoneHormone()+"' ");
		sql.append(",hithormone_area='"+hithormone.getHithormoneArea()+"' ");
		sql.append(",hithormone_value='"+hithormone.getHithormoneValue()+"' ");
		sql.append(" WHERE hithormone_id="+hithormone.getHithormoneId());

		System.out.println(sql.toString());
		try(Statement stmt = conn.createStatement()){
			stmt.executeUpdate(sql.toString());

		}catch(Exception ex){
			ex.printStackTrace();
			throw ex;
		}
	}

	protected void delete(Connection conn, String hithormoneIds)throws Exception{

		StringBuilder sql = new StringBuilder();
		sql.append("DELETE FROM hithormone  ");

		sql.append(" WHERE hithormone_id IN ("+hithormoneIds+")");
		System.out.println(sql.toString());
		try(Statement stmt = conn.createStatement()){
			stmt.executeUpdate(sql.toString());
		}catch(Exception ex){
			ex.printStackTrace();
			throw ex;
		}
	}


	protected Hithormone searchById(Connection conn,int id)throws Exception{

		StringBuilder sql = new StringBuilder();
		sql.append("SELECT ");
		sql.append("hithormone.hithormone_Id, ");
		sql.append("hithormone.hithormone_date, ");
		sql.append("hithormone.hithormone_hormone, ");
		sql.append("hithormone.hithormone_area, ");
		sql.append("hithormone.hithormone_value, ");
		sql.append("planting.planting_status ");
		sql.append("FROM ");
		sql.append("hithormone ");
		sql.append("Inner Join planting ON planting.planting_id = hithormone.hithormone_area ");
		sql.append("WHERE hithormone_Id ="+id);
		Hithormone hithormone = null;
		try(Statement stmt = conn.createStatement()){
			  ResultSet rs = stmt.executeQuery(sql.toString());
			  hithormone = new Hithormone();
		        if(rs.next()) {
		        	hithormone.setHithormoneId(rs.getString("hithormone_id"));
		        	hithormone.setHithormoneArea(rs.getString("hithormone_area")+","+rs.getString("planting_status"));
		        	hithormone.setHithormoneHormone(rs.getString("hithormone_hormone"));
		        	hithormone.setHithormoneDate(dateForShow(rs.getDate("hithormone_date")));
		        	hithormone.setHithormoneValue(rs.getString("hithormone_value"));
		        	hithormone.setPlantingStatus(rs.getString("planting_status"));
		        }
		}catch(Exception ex){
			ex.printStackTrace();
			throw ex;
		}
		return hithormone;
	}

	protected int countNumb(Connection conn , String areaId) throws Exception{
 		StringBuilder sql = new StringBuilder();
 		int i = 0;
 		sql.append("SELECT COUNT(hithormone.hithormone_area) AS tot ");
 		sql.append("FROM hithormone ");
 		sql.append("WHERE hithormone.hithormone_area ='"+areaId+"' ");
 		System.out.println(sql.toString());
 		try(Statement stmt = conn.createStatement()){
 			ResultSet rs = stmt.executeQuery(sql.toString());
 			if(rs.next()) {
 			   i = rs.getInt("tot");
 			}
 		}catch(Exception ex){
 			ex.printStackTrace();
 			throw ex;
 		}
 		return i;
     }

	protected Map<String,String> searchComboPlantAndArea(Connection conn) throws Exception{
		StringBuilder sql = new StringBuilder();
		Map<String,String> lstArea = new LinkedHashMap<String, String>();
		sql.append("SELECT a.area_name,p.planting_id,a.area_id,p.planting_status ");
		sql.append("FROM planting p INNER JOIN area a ON p.area_id = a.area_id ");
		sql.append("WHERE p.planting_status=3 or p.planting_status=6 or p.planting_status=2 or p.planting_status=5 ");
		try(Statement stmt = conn.createStatement()){
			ResultSet rs = stmt.executeQuery(sql.toString());
	        while(rs.next()) {
	        	lstArea.put(rs.getString("planting_id")+","+rs.getString("planting_status"), rs.getString("area_name"));
	        }
		}catch(Exception ex){
			ex.printStackTrace();
			throw ex;
		}
		return lstArea;
	}


	protected void updateStatus(Connection conn,String plantingId,int status)throws Exception{
		StringBuilder sql = new StringBuilder();
		sql.append("UPDATE  planting SET ");
		sql.append("planting_status='"+status+"' ");
		sql.append(" WHERE planting_id='"+plantingId+"' ");
		System.out.println(sql.toString());
		try(Statement stmt = conn.createStatement()){
			stmt.executeUpdate(sql.toString());
		}catch(Exception ex){
			ex.printStackTrace();
			throw ex;
		}

	}

	public static String dateForShow(Date date){
		String returnValue = null;
		try {
			if(date != null){
				returnValue = convertToString(date,"dd/MM/yyyy");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return returnValue;
	}

	public static String convertToString(Date date, String format) {
		if (date != null) {
			SimpleDateFormat formatDate = new SimpleDateFormat(format.toString(),Locale.ENGLISH);
			StringBuilder fdate = new StringBuilder(formatDate.format(date));
			return fdate.toString();
		} else {
			return "";
		}
	}


	public static String dateForInsert(String date){
		String returnValue = null;
		SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy", new Locale("en", "EN"));
		try {
			if(date != null && !date.trim().equals("")){
				returnValue = convertToString(sdf.parse(date),"yyyy-MM-dd");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return returnValue;
	}


    protected int countStatus(Connection conn , String areaId) throws Exception{
		StringBuilder sql = new StringBuilder();
		int i = 0;
		sql.append("SELECT COUNT(*) AS tot FROM planting  ");
		sql.append("WHERE planting.planting_status = '3' or planting.planting_status = '6' ");
		sql.append("And area_id = '"+areaId+"' ");
		try(Statement stmt = conn.createStatement()){
			ResultSet rs = stmt.executeQuery(sql.toString());
			if(rs.next()) {
			   i = rs.getInt("tot");
			}
		}catch(Exception ex){
			ex.printStackTrace();
			throw ex;
		}
		return i;
    }

}
